package com.tong.embeddedvr.websocket;

import com.tong.embeddedvr.constant.ConstantPool;
import com.tong.embeddedvr.service.impl.WebSSHServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.*;

import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;


/**
 * TODO
 *
 * @author gcl
 * @version 1.0
 * @Description
 * @date 2021/3/4 17:18
 */
@Component
public class WebSocketHandler implements org.springframework.web.socket.WebSocketHandler {

    @Autowired
    WebSSHServiceImpl webSSHService;

    private final Logger logger = LoggerFactory.getLogger(WebSocketHandler.class);

    /**
     * @Description 用户连接上webSocket的回调
     * @Author gcl
     * @Date 2021/3/4 17:24
     * @param: webSocketSession
     * @Return void
     */
    @Override
    public void afterConnectionEstablished(WebSocketSession webSocketSession) throws Exception {
        logger.info("用户[{}]请求连接主机",webSocketSession.getAttributes().get(ConstantPool.WEBSSH_USER_UUID));
        webSSHService.initConnection(webSocketSession);
    }

    /**
     * @Description 处理WebSocket接收到的信息的回调
     * @Author gcl
     * @Date 2021/3/4 17:34
     * @param: webSocketSession
     @param: webSocketMessage
      * @Return void
     */
    @Override
    public void handleMessage(WebSocketSession webSocketSession, WebSocketMessage<?> webSocketMessage) throws Exception {
        if (webSocketMessage instanceof TextMessage) {  //文本信息
            logger.info("用户:[{}],发送命令:[{}]", webSocketSession.getAttributes().get(ConstantPool.WEBSSH_USER_UUID), webSocketMessage.toString());
            //调用service接收消息
            webSSHService.recvHandler(((TextMessage) webSocketMessage).getPayload(), webSocketSession);
        } else if (webSocketMessage instanceof BinaryMessage) {  //二进制信息

        } else if (webSocketMessage instanceof PongMessage) {  //pong用于回应ping帧

        } else {  //无法识别信息类型
            logger.error("无法识别的信息:" + webSocketMessage);
        }
    }

    /**
     * @Description 错误回调
     * @Author gcl
     * @Date 2021/3/4 17:34
     * @param: webSocketSession
 @param: throwable
      * @Return void
     */
    @Override
    public void handleTransportError(WebSocketSession webSocketSession, Throwable throwable) throws Exception {
        logger.error("数据传输错误");
    }

    /**
     * @Description 连接关闭后的回调
     * @Author gcl
     * @Date 2021/3/4 17:34
     * @param: webSocketSession
 @param: closeStatus
      * @Return void
     */
    @Override
    public void afterConnectionClosed(WebSocketSession webSocketSession, CloseStatus closeStatus) throws Exception {
        logger.info("用户:[{}]关闭连接",(String)webSocketSession.getAttributes().get(ConstantPool.WEBSSH_USER_UUID));
        webSSHService.closeConnection(webSocketSession);
    }


    @Override
    public boolean supportsPartialMessages() {
        return false;
    }
}
